package org.xbill.DNS;

import java.net.InetAddress;
import java.net.UnknownHostException;

public final class ReverseMap
{
  private static Name inaddr4 = Name.fromConstantString("in-addr.arpa.");
  private static Name inaddr6 = Name.fromConstantString("ip6.arpa.");

  public static Name fromAddress(String paramString)
    throws UnknownHostException
  {
    byte[] arrayOfByte = Address.toByteArray(paramString, 1);
    if (arrayOfByte == null)
      arrayOfByte = Address.toByteArray(paramString, 2);
    if (arrayOfByte == null)
      throw new UnknownHostException("Invalid IP address");
    return fromAddress(arrayOfByte);
  }

  public static Name fromAddress(String paramString, int paramInt)
    throws UnknownHostException
  {
    byte[] arrayOfByte = Address.toByteArray(paramString, paramInt);
    if (arrayOfByte == null)
      throw new UnknownHostException("Invalid IP address");
    return fromAddress(arrayOfByte);
  }

  public static Name fromAddress(InetAddress paramInetAddress)
  {
    return fromAddress(paramInetAddress.getAddress());
  }

  public static Name fromAddress(byte[] paramArrayOfByte)
  {
    int k;
    if ((paramArrayOfByte.length != 4) && (paramArrayOfByte.length != 16))
      throw new IllegalArgumentException("array must contain 4 or 16 elements");
    StringBuffer localStringBuffer = new StringBuffer();
    if (paramArrayOfByte.length == 4)
    {
      k = paramArrayOfByte.length - 1;
      if (k < 0);
    }
    try
    {
      int[] arrayOfInt;
      int i;
      do
      {
        while (true)
        {
          if (paramArrayOfByte.length != 4)
            break label193;
          Name localName2 = Name.fromString(localStringBuffer.toString(), inaddr4);
          localObject = localName2;
          return localObject;
          localStringBuffer.append(0xFF & paramArrayOfByte[k]);
          if (k > 0)
            localStringBuffer.append(".");
          --k;
        }
        arrayOfInt = new int[2];
        i = paramArrayOfByte.length - 1;
      }
      while (i < 0);
      arrayOfInt[0] = ((0xFF & paramArrayOfByte[i]) >> 4);
      arrayOfInt[1] = (0xF & 0xFF & paramArrayOfByte[i]);
      int j = arrayOfInt.length - 1;
      while (true)
      {
        while (j < 0)
          --i;
        localStringBuffer.append(Integer.toHexString(arrayOfInt[j]));
        if ((i > 0) || (j > 0))
          localStringBuffer.append(".");
        --j;
      }
      label193: Name localName1 = Name.fromString(localStringBuffer.toString(), inaddr6);
      Object localObject = localName1;
    }
    catch (TextParseException localTextParseException)
    {
      throw new IllegalStateException("name cannot be invalid");
    }
  }

  public static Name fromAddress(int[] paramArrayOfInt)
  {
    byte[] arrayOfByte = new byte[paramArrayOfInt.length];
    int i = 0;
    while (true)
    {
      if (i >= paramArrayOfInt.length)
        return fromAddress(arrayOfByte);
      if ((paramArrayOfInt[i] < 0) || (paramArrayOfInt[i] > 255))
        throw new IllegalArgumentException("array must contain values between 0 and 255");
      arrayOfByte[i] = (byte)paramArrayOfInt[i];
      ++i;
    }
  }
}